package com.fenger.bus.controller;


import com.fenger.bus.service.CustomerService;
import com.fenger.bus.vo.CustomerVo;
import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.utils.DataGirdView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CustomerController {
    private final static String CUSTOMER_PREFIX = "customer/";

    @Autowired
    CustomerService customerService;

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "loadAllCustomer")
    private DataGirdView loadAllCustomer(CustomerVo customerVo) {
        return this.customerService.queryAllCustomer(customerVo);
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "addCustomer")
    private DataGirdView addCustomer(CustomerVo customerVo) {
        try {
            this.customerService.addCustomer(customerVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "updateCustomer")
    private DataGirdView updateCustomer(CustomerVo customerVo) {

        try {
            this.customerService.updateCustomer(customerVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteCustomer")
    private DataGirdView deleteCustomer(CustomerVo customerVo) {

        try {
            this.customerService.deleteCustomer(customerVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteListCustomer")
    private DataGirdView deleteListCustomer(@RequestParam(value="id[]", required = false) String[] id) {
        System.out.println("--------------------");
        System.out.println(id);
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }

}
